package com.flyduck.vueshop.cart.h5.controller;

import cn.hutool.core.collection.ListUtil;
import com.flyduck.vueshop.cart.h5.service.H5CartItemService;
import com.flyduck.vueshop.cart.h5.vo.H5CartItemAddReqVO;
import com.flyduck.vueshop.cart.h5.vo.H5CartItemRespVO;
import com.flyduck.vueshop.common.core.lang.Result;
import com.flyduck.vueshop.common.sotoken.annotation.Login;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @program: vueshop
 * @description:
 * @author: flyduck
 * @create: 2024-05-19 09:26
 **/
@RestController
@RequestMapping("/cart/h5/cartItem")
public class H5CartItemController {

    @Resource
    private H5CartItemService h5CartItemService;

    @Login
    @GetMapping("/getCartItemList")
    public Result<List<H5CartItemRespVO>> getCartItemList(){
        List<H5CartItemRespVO> cartItemResponseList = h5CartItemService.getCartItemList();
        return Result.success(cartItemResponseList);
    }

    @Login
    @GetMapping("/getTotalCount")
    public Result<Long> getTotalCount(){
        Long count = h5CartItemService.getTotalCount();
        return Result.success(count);
    }

    @Login
    @PostMapping("/getTotalAmountByIds")
    public Result<BigDecimal> getTotalAmountByIds(@RequestBody Long[] ids){
        BigDecimal totalAmount = h5CartItemService.getTotalAmountByIds(ListUtil.toList(ids));
        return Result.success(totalAmount);
    }

    @Login
    @PostMapping("/updateQuantityById")
    public Result<Void> updateQuantity(Long id,Integer quantity){
        h5CartItemService.updateQuantityById(id,quantity);
        return Result.success();
    }

    @Login
    @PostMapping("addCartItem")
    public Result<Void> addCartItem(@Validated @RequestBody H5CartItemAddReqVO h5CartItemAddReqVO){
        h5CartItemService.addCartItem(h5CartItemAddReqVO);
        return Result.success();
    }

    @Login
    @PostMapping("batchDeleteByIds")
    public Result<Void> batchDeleteByIds(@RequestBody Long[] ids){
        h5CartItemService.batchDeleteByIds(ids);
        return Result.success();
    }
}
